<?php

$args = array();
foreach ($_SERVER['argv'] as $arg) {
    if (strpos($arg, '--') === 0) {
        if (strpos($arg, ':') > -1) {
            list($k, $v) = explode(':', $arg);
        } else {
            $k = $arg;
            $v = null;
        }
        $args[$k] = $v;
    } else {
        exit('ERROR Parameter ' . $arg);
    }
}

$PARAM = array();

// 定义运行于开发机，采用Config中开发机的相关定义
if (array_key_exists('--ondev', $args)) {
    define('ONDEV', true);
    $_SERVER['HTTP_HOST'] = 'dev.app.baobaot.com';
} else {
    define('ONDEV', false);
    $_SERVER['HTTP_HOST'] = 'app.baobaot.com';
}

// 定义运行网站使用的域名
if (array_key_exists('--host', $args)) {
    $PARAM['host'] = $_SERVER['HTTP_HOST'] = $args['--host'];
} else {
    $PARAM['host'] = null;
}

// 定义时间日期
if (array_key_exists('--date', $args)) {
    $PARAM['date'] = strtotime($args['--date']);
} else {
    $PARAM['date'] = null;
}

// 默认设置
$log_file = '/data/logs/ark_cron.log';           // 计划任务执行信息记录到的log文件默认地址
$log_email = 'wangrunshi@chamerfood.com';                  // 定义计划任务发生错误时报告错误日志的默认邮箱
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['SERVER_SOFTWARE'] = 'PHP CLI';
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['QUERY_STRING'] = '';
$_SERVER['PHP_SELF'] = $_SERVER['REQUEST_URI'] = basename($_SERVER['SCRIPT_FILENAME']);

// 初始化框架
if (ONDEV) {
    define('ANPHP_PATH', '/data/wwwroot/AppcenterFramework/trunk');
} else {
    define('ANPHP_PATH', '/data/www/AppcenterFramework/trunk');
}
require ANPHP_PATH . '/init.php';

error_reporting(E_ALL);

define('ROOT_PATH', substr(dirname(__FILE__), 0, -14));
define('SCRIPT_PATH', ROOT_PATH);
define('DATA_PATH', SCRIPT_PATH . '/data');
define('MODULE_PATH', SCRIPT_PATH . '/module');
define('MODULE_CORE', MODULE_PATH . '/core');

require ROOT_PATH . '/config/config.php';
// require_once ROOT_PATH.'/helper/setup.php';

if (defined('CRON_QUEUE_SMS_LOG')) {
    $log_file = RUN_LOG;
}
if (defined('SCRIPT_EMAIL')) {
    $log_email = SCRIPT_EMAIL;
}

// 加载公用函数库
require_once ROOT_PATH . '/helper/common.php';
require_once MODULE_CORE . '/helper/common.php';